package com.scse.bms.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;


/**
 * @Title: DownloadMapper
 * @Author: 吴赠禹
 * @Date: 2021/2/24 21:02
 * @Version: 1.0
 * @Description: 操作t_download
 */


@Repository
@Mapper
public interface DownloadMapper {


    /**
     * 记录下载行为
     * @param userId
     * @param bookId
     * @return
     */
    @Insert("insert into t_download(user_id, book_id, theme_id) values(#{userId}, #{bookId}, #{themeId})")
    int insertDownload(@Param("userId") Integer userId, @Param("bookId") Integer bookId, @Param("themeId") Integer themeId);


    /**
     * 统计一天内下载次数，用于控制用户下载次数
     * 注意count()函数返回null，所以要加if函数，空则返回0
     */
    @Select("select count(time) from t_download where user_id = #{userId} and left(time, 10) = CURDATE() group by CURDATE()")
    Integer selectDownCountByToday(Integer userId);

}
